//
//  LoginViewController.m
//  QMCXDriver
//
//  Created by Massimo on 2017/4/12.
//  Copyright © 2017年 Massimo. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginInputView.h"
#import "QLoginButton.h"
@interface LoginViewController ()

@property (nonatomic, copy)NSString *phone;
@property (nonatomic, copy)NSString *password;

@property (nonatomic, copy)LoginSuccess loginBlock;
@property (nonatomic, strong)id userInfo;
@property (nonatomic, weak)IBOutlet LoginInputView *phoneInputView;
@property (nonatomic, weak)IBOutlet LoginInputView *pwInputView;
@property (nonatomic, weak)IBOutlet QLoginButton *loginButton;
@property (nonatomic, weak)IBOutlet UILabel *serviceProtocol;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    __weak typeof(self) weakSelf = self;
    [_phoneInputView setTextChange:^(NSString *text) {
        NSLog(@" phone : %@",text);
        weakSelf.loginButton.enabled = (text.length == 11);
        weakSelf.phone = text;
    }];
    [_pwInputView setTextChange:^(NSString *text) {
        NSLog(@" pw : %@",text);
        weakSelf.password = text;
    }];
    
    self.serviceProtocol.userInteractionEnabled = YES;
    [self.serviceProtocol addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapProtocol:)]];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)setLoginSuccess:(LoginSuccess)success{
    self.loginBlock = success;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


- (void)tapProtocol:(id)sender {
    
    [ProgressHUD showSuccess:@"服务协议被点击了"];
    
}


- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)login:(id)sender {
    if (![QTools isValidateMobile:self.phone]){
        [ProgressHUD showError:@"请输入正确的手机号码"];
        return;
    }
    if (!self.password.length || self.password.length < 6) {
        [ProgressHUD showError:@"密码长度不能小于6位,请输入正确的密码"];
        return;
    }
    
    
    [self requestForLogin];
    
}


- (void)loginSuccess{
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.loginBlock) {
            self.loginBlock(self.userInfo);
        }
    }];
}
- (void)loginError:(NSString *)errorMsg{
    [ProgressHUD showError:errorMsg];
}


- (void)requestForLogin{
    
    [ProgressHUD show:@"正在登录，请稍等..." Interaction:NO];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [ProgressHUD dismiss];
        [self loginSuccess];
    });
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
